/**
 * 
 */
package com.autoscript.ui.core.action;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;

import javax.swing.Icon;
import javax.swing.KeyStroke;

import com.autoscript.ui.core.BatchUI;
import com.autoscript.ui.core.filter.XmlFilenameFilter;
import com.autoscript.ui.helper.SerialHelper;
import com.autoscript.ui.helper.StringHelper;
import com.autoscript.ui.helper.UIPropertyHelper;

/**
 * 保存工程Action
 * 作者:龙色波
 * 日期:2013-10-9
 */
public class SaveProjectAction extends AbstractUIAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public SaveProjectAction(BatchUI ui){
		super(ui);
	}
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String fileName;
		//文件名为空，弹出对话框
		if(StringHelper.isEmpty(ui.getProjectFileName())){
			FileDialog dlg;
			dlg = new FileDialog(ui.getFrame(),UIPropertyHelper.getString("dialog.saveproject.title"),FileDialog.SAVE);
			dlg.setFilenameFilter(new XmlFilenameFilter());
			dlg.setVisible(true);
			
			fileName = dlg.getDirectory()+dlg.getFile();
		}else{
			fileName = ui.getProjectFileName();
		}
		if(fileName!=null){
			//先同步更新模型
			ui.syncTreeNode(ui.getSelectProjectTreeNode());
			try {
				SerialHelper.saveToXml(fileName, ui.getProjectModel());
			} catch (FileNotFoundException e1) {
				logger.error(e1.getMessage(), e1);
				e1.printStackTrace();
			}
		}
	}
	public SaveProjectAction(String text, Icon icon,char accelerator,String description) {
		    super(text, icon);

		    putValue("AcceleratorKey", KeyStroke.getKeyStroke(accelerator));

		    putValue("ShortDescription", description);
		  }
}
